//
//  SINATecScoreQuerDetailItemViewController.m
//  sinashebao
//
//  Created by  androidlongs on 2018/1/8.
//  Copyright © 2018年  androidlongs. All rights reserved.
//  学生成绩列表

#import "SINATecScoreQuerDetailItemViewController.h"
#import "SINATecManagerQuerScoreCourseItem2TableViewCell.h"

@interface SINATecScoreQuerDetailItemViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
//
@property(nonatomic,strong) NSMutableArray *dataList;
@property (weak, nonatomic) IBOutlet UILabel *noDataLabel;

@property(strong,nonatomic) SINAStuSchoolYearModel *currentYearModel;
@property(strong,nonatomic) SINATecCourseModel *userModel;


@property(nonatomic,assign) NSInteger oldY;
@end

@implementation SINATecScoreQuerDetailItemViewController


-(NSMutableArray *)dataList{
    if (!_dataList) {
        _dataList = [NSMutableArray array];
    }
    return  _dataList;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.backgroundColor=[UIColor colorWithHexString:@"#fafafa"];
    self.tableView.scrollEnabled=NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    for (NSInteger i=0; i<0; i++) {
        [self.dataList addObject:@"test"];
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noti1) name:@"noti1" object:nil];
    
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.noDataLabel andWithBlick:^(UIView *view) {
        if (self.currentYearModel!=nil&&self.dataList.count==0) {
             [self loadingSchoolYearSubScoreListNetData:self.currentYearModel andWithUser:self.userModel];
        }
    }];
}
-(void)noti1

{
    self.tableView.scrollEnabled=YES;
    DLog(@"接收 不带参数的消息");
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if ([scrollView isEqual: self.tableView]) {
    
        if (self.tableView.contentOffset.y >self.oldY) {
            // 上滑
            DLog(@"上滑");
            if (self.prentScrollView.contentOffset.y==0) {
                self.tableView.scrollEnabled=NO;
            } else {
                self.tableView.scrollEnabled=YES;
            }
            
        }
        else{
            // 下滑
            DLog(@"下滑");
            
            if (self.prentScrollView.contentOffset.y>0) {
                self.tableView.scrollEnabled=NO;
            } else {
                self.tableView.scrollEnabled=YES;
            }
        }
        if (self.tableView.contentOffset.y ==0) {
            self.tableView.scrollEnabled=NO;
        } else {
            
        }
        
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    // 获取开始拖拽时tableview偏移量
    self.oldY = self.tableView.contentOffset.y;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataList.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    SINATecManagerQuerScoreCourseItem2TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SINATecManagerQuerScoreCourseItem2TableViewCell"];
    
    if (cell == nil) {
        cell= [[[NSBundle mainBundle]loadNibNamed:@"SINATecManagerQuerScoreCourseItem2TableViewCell" owner:nil options:nil] firstObject];
        
    }
    SINATecCourseModel *model = self.dataList[indexPath.row];
    cell.model=model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell ;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    SINATecScoreQueryDetailViewController *controller = [[SINATecScoreQueryDetailViewController alloc]initWithNibName:@"SINATecScoreQueryDetailViewController" bundle:nil];
//    [self.navigationController pushViewController:controller animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


//加载成绩详情

-(void)loadingStudentScoreList:(SINAStuSchoolYearModel *)yearModel andWithUser:(SINATecCourseModel *)userModel{
    [self.dataList removeAllObjects];
    self.currentYearModel = yearModel;
    self.userModel=userModel;
    [self loadingSchoolYearSubScoreListNetData:yearModel andWithUser:userModel];
}

//加载成绩详情
-(void)loadingSchoolYearSubScoreListNetData:(SINAStuSchoolYearModel *)yearModel andWithUser:(SINATecCourseModel *)userModel{
    
    
    if (yearModel==nil) {
//        [YPXToastView showToastViewWithText:@"学年信息异常" andDuration:1 andParentView:self.view];
        return;
    }
    
    [self startLoading];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    //学生uuid String useruuid
    [dict setObject:userModel.uuid forKey:@"useruuid"];
    //学年 String schoolyear
    [dict setObject:yearModel.schoolyear forKey:@"schoolyear"];
    //学期 String term
    [dict setObject:yearModel.term forKey:@"term"];
    
    WeakSelf;
    [CXAFNHttpRequestUtils httpGetRequest:[NSString stringWithFormat:@"%@%@",baseUrl,getSubjectScoreByUserInfoUrl] withParamters:dict success:^(id responseData) {
        [weakSelf stopLoading];
        NSString *code = responseData[@"code"];
        
        if ([code isEqualToString:@"1000"]) {
            NSArray *result = responseData[@"content"];
            if (result==nil||result.count==0) {
                self.noDataLabel.hidden=NO;
                self.tableView.hidden=YES;
            } else {
                //有成绩信息
                for(NSInteger i=0;i<result.count;i++){
                    NSDictionary *dic = result[i];
                    SINATecCourseModel *model = [[SINATecCourseModel alloc]initWithDict:dic];
                    [self.dataList addObject:model];
                }
                self.noDataLabel.hidden=YES;
                self.tableView.hidden=NO;
            }
            
            
        }else{
            
            NSString *message =responseData[@"message"];
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定"
                                                                 cancleBtn:nil];
            [alertView showXLAlertView];
            
            self.noDataLabel.hidden=NO;
            self.tableView.hidden=YES;
        }
        
        [self.tableView reloadData];
        
        
        
    } failure:^(NSError *error, NSInteger errCode) {
        DLog(@"加载 失败  %@ ",error.debugDescription);
        [weakSelf stopLoading];
        self.noDataLabel.hidden=NO;
        self.tableView.hidden=YES;
       
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"加载数据失败" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
         [self.tableView reloadData];
    }];
}
@end
